package user.classify;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import JDBC.DatabaseDao;
import Utils.DateUtils;
import Utils.Printers;

public class TopicCount {

	public static void countTopic() {
		Hashtable<String, Integer> topic_count = new Hashtable<String, Integer>();
		Hashtable<String, String> topic_ids = new Hashtable<String, String>();
		String sql = "";
		System.out.println("start:\n" + DateUtils.date2Str(new Date(), "-"));
		try {
			int pageSize = 1000;
			int curPage = 0;
			for (curPage = 0; curPage < 20; curPage++) {
				sql = "SELECT TOP " + pageSize + "* FROM statuses WHERE (status_id NOT IN   (SELECT TOP " + (pageSize*curPage) + " status_id FROM statuses ORDER BY status_id))ORDER BY status_id";

				Statement stmt = DatabaseDao.getConnection().createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				while (rs.next()) {
					int id = rs.getInt("status_id");
					String text = rs.getString("content");

					String topic = "";
					Pattern p = Pattern.compile("#.*#");
					Matcher matcher = p.matcher(text);
					boolean result = matcher.find();
					while (result) {
						topic = matcher.group();
						result = matcher.find();
					}

					// int index = text.substring(1).indexOf("#");
					// String topic = text.substring(1, index + 1);
					// System.out.println(id + "," + text + "," + topic);

					if (topic_count.containsKey(topic))
						topic_count.put(topic, topic_count.get(topic) + 1);
					else
						topic_count.put(topic, 1);

					String ids = topic_ids.get(topic);
					if (topic_ids.containsKey(topic)) {
						ids += "," + id;
						topic_ids.put(topic, ids);
					} else
						topic_ids.put(topic, String.valueOf(id));
				}

			}
			System.out.println("count ok:\n" + DateUtils.date2Str(new Date(), "-"));
			// System.out.println("print counts");
			Printers.htPrinters(topic_count);
			// System.out.println("print ids");
			Printers.htPrinters(topic_ids);
			Enumeration en = topic_count.keys();
			int count = 0;
			while (en.hasMoreElements()) {
				Object t = en.nextElement();
				count++;
				//sql = "insert into test_topic values(" + count + ",'" + t + "'," + topic_count.get(t) + ",'" + topic_ids.get(t) + "')";
				sql = "insert into test_topic values(" + count + ",'" + t + "'," + topic_count.get(t) + ")";
				DatabaseDao.updateData(sql);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) throws FileNotFoundException {
	     /*File test = new File("D:\\test_topic.txt");   
	     PrintStream out = new PrintStream(new FileOutputStream(test));   
	     System.setOut(out);*/ 
		
		TopicCount.countTopic();
		System.out.println("ok");
		// Random rand = new Random();
		// for (int i = 0; i < 200000; i++) {
		// String sql = "insert into test_statuses values(" +
		// rand.nextInt(65535) + ", '#123#" + rand.nextFloat() + "')";
		// DatabaseDao.updateData(sql);
		// }
	}

}
